home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / IFC_112 / netscape / application / FontMetrics.java < prev    next >
Encoding:
Text File  |  1999-05-28  |  7.3 KB  |  262 lines  |  [TEXT/CWIE]

  1. // FontMetrics.java
  2. // By Ned Etcode
  3. // Copyright 1995, 1996, 1997 Netscape Communications Corp.  All rights reserved.
  4.  
  5. package netscape.application;
  6.  
  7. import netscape.util.*;
  8.  
  9. /** Object subclass that manages a Font instance's metrics.<p>
  10.   * <i><b>Note:</b> FontMetrics are currently retrieve using the
  11.   * default java.awt.Toolkit, so they contain screen metric information
  12.   * only.</i>.
  13.   */
  14. public class FontMetrics {
  15.     Font                        _font;
  16.     java.awt.FontMetrics        _awtMetrics;
  17.  
  18.     final static String         LEADING = "Leading", ASCENT = "Ascent",
  19.                                 DESCENT = "Descent",
  20.                                 TOTAL_HEIGHT = "Total Height",
  21.                                 MAX_ASCENT = "Maximum Ascent",
  22.                                 MAX_DESCENT = "Maximum Descent",
  23.                                 MAX_ADVANCE = "Maximum Advance";
  24.  
  25.     /** Constructs a FontMetrics not associated with a Font. This method
  26.       * is only useful when decoding.
  27.       */
  28.     public FontMetrics() {
  29.         super();
  30.     }
  31.  
  32.     /** Creates a FontMetrics associated with <b>aFont</b>.
  33.       */
  34.     public FontMetrics(Font aFont) {
  35.         this();
  36.  
  37.         _font = aFont;
  38.         if (!_font.wasDownloaded()) {
  39.             _awtMetrics =
  40.                 AWTCompatibility.awtToolkit().getFontMetrics(_font._awtFont);
  41.         }
  42.     }
  43.  
  44.     FontMetrics(java.awt.FontMetrics awtMetrics) {
  45.         this();
  46.  
  47.         _font = AWTCompatibility.fontForAWTFont(awtMetrics.getFont());
  48.         _awtMetrics = awtMetrics;
  49.     }
  50.  
  51.     /** Returns the Font associated with this FontMetrics.
  52.       */
  53.     public Font font() {
  54.         return _font;
  55.     }
  56.  
  57.     /** Returns the standard leading (line spacing) for the Font, the logical
  58.       * amount of space to be reserved between the descent of one line of
  59.       * text and the ascent of the next line. The height metric is calculated
  60.       * to include this extra space.
  61.       */
  62.     public int leading() {
  63.         if (_awtMetrics != null) {
  64.             return _awtMetrics.getLeading();
  65.         }
  66.  
  67.         return _font._intValueFromDescription(LEADING);
  68.     }
  69.  
  70.     /** Returns the Font's ascent, the distance from the baseline to the
  71.       * top of the characters.
  72.       */
  73.     public int ascent() {
  74.         if (_awtMetrics != null) {
  75.             return _awtMetrics.getAscent();
  76.         }
  77.  
  78.         return _font._intValueFromDescription(ASCENT);
  79.     }
  80.  
  81.     /** Returns the Font's descent, the distance from the baseline to the
  82.       * bottom of the characters.
  83.       */
  84.     public int descent() {
  85.         if (_awtMetrics != null) {
  86.             return _awtMetrics.getDescent();
  87.         }
  88.  
  89.         return _font._intValueFromDescription(DESCENT);
  90.     }
  91.  
  92.     /** Returns the Font's total height, the distance between the baseline
  93.       * of adjacent lines of text (leading + ascent + descent).
  94.       */
  95.     public int height() {
  96.         if (_awtMetrics != null) {
  97.             return _awtMetrics.getHeight();
  98.         }
  99.  
  100.         return _font._intValueFromDescription(TOTAL_HEIGHT);
  101.     }
  102.  
  103.     /** Returns the Font's total character height, the distance from the
  104.       * bottom of the descent to the top of the ascent.
  105.       */
  106.     public int charHeight() {
  107.         if (_awtMetrics != null) {
  108.             return _awtMetrics.getAscent() + _awtMetrics.getDescent();
  109.         }
  110.  
  111.         return _font._intValueFromDescription(ASCENT) +
  112.                _font._intValueFromDescription(DESCENT);
  113.     }
  114.  
  115.     /** Returns the maximum ascent of all characters in this Font. No
  116.       * character extends further above the baseline than this metric.
  117.       */
  118.     public int maxAscent() {
  119.         if (_awtMetrics != null) {
  120.             return _awtMetrics.getMaxAscent();
  121.         }
  122.  
  123.         return _font._intValueFromDescription(MAX_ASCENT);
  124.     }
  125.  
  126.     /** Returns the maximum descent of all characters in this Font. No
  127.       * character descends futher below the baseline than this metric.
  128.       */
  129.     public int maxDescent() {
  130.         if (_awtMetrics != null) {
  131.             return _awtMetrics.getMaxDecent();
  132.         }
  133.  
  134.         return _font._intValueFromDescription(MAX_DESCENT);
  135.     }
  136.  
  137.     /** Returns the maximum advance width of any character in this Font,
  138.       * or <b>-1</b> if unknown.
  139.       */
  140.     public int maxAdvance() {
  141.         if (_awtMetrics != null) {
  142.             return _awtMetrics.getMaxAdvance();
  143.         }
  144.  
  145.         return _font._intValueFromDescription(MAX_ADVANCE);
  146.     }
  147.  
  148.     /** Returns <b>aChar</b>'s width in the FontMetric's Font.
  149.       */
  150.     public int charWidth(int aChar) {
  151.         if (_awtMetrics != null) {
  152.             return _awtMetrics.charWidth(aChar);
  153.         }
  154.  
  155.         return 0;
  156.     }
  157.  
  158.     /** Returns <b>aChar</b>'s width in the FontMetric's Font.
  159.       */
  160.     public int charWidth(char aChar) {
  161.         if (_awtMetrics != null) {
  162.             return _awtMetrics.charWidth(aChar);
  163.         }
  164.  
  165.         return 0;
  166.     }
  167.  
  168.     /** Returns <b>aString</b>'s width in the FontMetric's Font.
  169.       */
  170.     public int stringWidth(String aString) {
  171.         int     length, i, index;
  172.  
  173.         if (aString == null) {
  174.             return 0;
  175.         } else if (_awtMetrics != null) {
  176.             return _awtMetrics.stringWidth(aString);
  177.         }
  178.  
  179.         length = 0;
  180.         for (i = 0; i < aString.length(); i++) {
  181.             index = (int)aString.charAt(i);
  182.             if (index < 0 || index >= _font._widthsArray.length) {
  183.                 continue;
  184.             }
  185.  
  186.             length += _font._widthsArray[index];
  187.         }
  188.  
  189.         return length;
  190.     }
  191.  
  192.     /** Returns the height of all strings in the FontMetric's Font.
  193.       * Equivalent to the following code:
  194.       * <pre>
  195.       *     ascent() + descent();
  196.       * </pre>
  197.       */
  198.     public int stringHeight() {
  199.         return ascent() + descent();
  200.     }
  201.  
  202.     /** Returns <b>aString</b>'s size in the FontMetric's Font.
  203.       * Equivalent to the following code:
  204.       * <pre>
  205.       *     new Size(stringWidth(aString), stringHeight());
  206.       * </pre>
  207.       */
  208.     public Size stringSize(String aString) {
  209.         return new Size(stringWidth(aString), stringHeight());
  210.     }
  211.  
  212.     /** Returns the width of <b>length</b> characters of the array
  213.       * <b>data</b> in the FontMetric's Font, starting at <b>offset</b> within
  214.       * the array.
  215.       */
  216.     public int charsWidth(char data[], int offset, int length) {
  217.         if (_awtMetrics != null) {
  218.             return _awtMetrics.charsWidth(data, offset, length);
  219.         }
  220.  
  221.         return 0;
  222.     }
  223.  
  224.     /** Returns the width of <b>length</b> bytes of the array <b>data</b>
  225.       * in the FontMetric's Font, starting at <b>offset</b> within the array.
  226.       */
  227.     public int bytesWidth(byte data[], int offset, int length) {
  228.         if (_awtMetrics != null) {
  229.             return _awtMetrics.bytesWidth(data, offset, length);
  230.         }
  231.  
  232.         return 0;
  233.     }
  234.  
  235.     /** Returns the widths of the first 256 characters in the FontMetric's
  236.       * Font.
  237.       */
  238.     public int[] widthsArray() {
  239.         if (_awtMetrics != null) {
  240.             return _awtMetrics.getWidths();
  241.         }
  242.  
  243.         return _font._widthsArray;
  244.     }
  245.  
  246.     /** Returns the index of the first glyph in a downloaded Font.
  247.       */
  248.     public int widthsArrayBase() {
  249.         return _font._widthsArrayBase;
  250.     }
  251.  
  252.     /** Returns the FontMetric's string representation.
  253.       */
  254.     public String toString() {
  255.         if (_awtMetrics != null) {
  256.             return _awtMetrics.toString();
  257.         }
  258.  
  259.         return "";
  260.     }
  261. }
  262.